package cn.kailangtu.dataspot.activify;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.fragment.NavHostFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import cn.kailangtu.commonevents.eventEntity.FragmentChangedEvent;
import cn.kailangtu.commonevents.eventEntity.OnFragmentChangeEvent;
import cn.kailangtu.commontools.AppVariable;
import cn.kailangtu.dataspot.R;
import cn.kailangtu.dataspot.adapter.ExceptionLogAdapter;
import cn.kailangtu.dataspot.data.ControlTree;
import cn.kailangtu.dataspot.data.ExceptionData;
import cn.kailangtu.dataspot.data.PageIndex;
import cn.kailangtu.dataspot.data.context.SelectContext;
import cn.kailangtu.dataspot.data.consts.ControlAction;
import cn.kailangtu.dataspot.databinding.FragmentExceptionLogLowBinding;
import cn.kailangtu.dataspot.status;
import cn.kailangtu.dataspot.tasks.BaseTask;

public class ExceptionLogLowFragment extends BaseFragment {

    private FragmentExceptionLogLowBinding binding;

    private ExceptionLogAdapter adapter = null;
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentExceptionLogLowBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().post(new FragmentChangedEvent(R.layout.fragment_exception_log_low));

        initListen();
        resetData();
    }

    private void resetData(){
        initData();
        if(status.logViewIndex == 1)
        {
            binding.tvExceptionInfoTitle.setText("故障信息");
        }
        else if(status.logViewIndex == 3)
        {
            binding.tvExceptionInfoTitle.setText("操作信息");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    protected void resetPageIndex() {
        status.localPageIndex = PageIndex.L_LOG;

        if (status.localToPlc != 2) {
            BaseTask.toNewPage(status.localPageIndex);
        }
    }

    @Override
    void resetPlatform() {
        if(AppVariable.platform_version == 450 && AppVariable.need_reset_view) {
            commonReset(binding.rlExceptionLogContainer);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OnFragmentChangeEvent event) {
        // Do something
        if(R.layout.fragment_factory_setting_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(ExceptionLogLowFragment.this)
                    .navigate(R.id.action_ExceptionLogLowFragment_to_FactorySettingLowFragment);
        }
        else if(R.layout.fragment_switch_test_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(ExceptionLogLowFragment.this)
                    .navigate(R.id.action_ExceptionLogLowFragment_to_SwitchTestLowFragment);
        }
        else if(R.layout.fragment_common_setting_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(ExceptionLogLowFragment.this)
                    .navigate(R.id.action_ExceptionLogLowFragment_to_CommonSettingLowFragment);
        }
        else if(R.layout.fragment_exception_log_low == event.getTargetFragmentId())
        {
            resetData();
        }
        else if(R.layout.fragment_real_picture_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(ExceptionLogLowFragment.this)
                    .navigate(R.id.action_ExceptionLogLowFragment_to_RealPictureLowFragment);
        }
    }

    private int pageIndex = 0;
    private void initData(){
        adapter = new ExceptionLogAdapter(this.getContext(), ExceptionData.getLogData(pageIndex, status.logViewIndex));
        binding.lvExceptionLog.setAdapter(adapter);
    }

    private void nextData(){
        pageIndex++;
        List<ExceptionData> ls =   ExceptionData.getLogData(pageIndex, status.logViewIndex);
        if(ls.size() == 0)
        {
            pageIndex--;
        }
        adapter.setData(ls);
        adapter.notifyDataSetChanged();
    }

    private void previewData(){
        pageIndex--;
        if(pageIndex < 0)
        {
            pageIndex = 0;
        }
        adapter.setData( ExceptionData.getLogData(pageIndex, status.logViewIndex));
        adapter.notifyDataSetChanged();
    }

    private void initListen(){
        binding.btnExceptionLogNextLow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nextData();
            }
        });
        binding.btnExceptionLogPreviewLow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                previewData();
            }
        });
    }



    @Override
    protected int processControlsEvent(int eventType) {
        if(eventType == ControlAction.Selection)
        {
            TextView textView =  this.getActivity().findViewById(controlTree.getSelectNode().getIndex());
            SelectContext.setSelectedView(textView);
        }
        else if(eventType == ControlAction.Entry){
            TextView textView =  this.getActivity().findViewById(controlTree.getSelectNode().getIndex());
            textView.callOnClick();
        }
        return 0;
    }

    @Override
    protected void buildControlTree() {
        controlTree = ControlTree.getLowExceptionLog();
        controlTree.setSelectNode(null);
    }


    @SuppressLint("ResourceAsColor")
    private void selectControl()
    {

    }
    private void entryControl()
    {
        Log.d("CONTROL","进入控件");
    }
}